package com.ys.everydaynews.ui.main.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.util.Log
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.view.MotionEventCompat
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.ys.everydaynews.R
import com.ys.everydaynews.databinding.ItemChannelsRecommendBinding
import com.ys.everydaynews.databinding.ItemMyChannelsBinding
import com.ys.everydaynews.databinding.ItemMyTitleBinding
import com.ys.everydaynews.databinding.ItemRecommendTitleBinding
import com.ys.everydaynews.ui.main.model.TabChModel
import com.ys.everydaynews.ui.main.model.TabListModel
import com.ys.everydaynews.view.OnItemMoveListener


class ChannelAdapter(mTabList: TabListModel, mItem: ItemTouchHelper) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>(), OnItemMoveListener {
    private val myChannels: MutableList<TabChModel> = mTabList.myChannels
    private val channelsRecommend: MutableList<TabChModel> = mTabList.channelsRecommend
    private var isEdit = false
    private val mItemHelper: ItemTouchHelper = mItem

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val layoutInflater: LayoutInflater = LayoutInflater.from(parent.context)
        when (viewType) {
            0 -> {
                val binding: ItemMyTitleBinding =
                    DataBindingUtil.inflate(layoutInflater, R.layout.item_my_title, null, false)
                binding.tvEdit.setOnClickListener {
                    if (!isEdit) {
                        starEdit(parent as RecyclerView)
                        binding.tvEdit.text = "完成"
                        binding.tvTitleGray.text = "拖动可排序"
                    } else {
                        canceEdit(parent as RecyclerView)
                        binding.tvEdit.text = "编辑"
                        binding.tvTitleGray.text = "点击进入频道"
                    }
                }
                return MyTitleViewHolder(binding)
            }
            1 -> {
                val binding: ItemRecommendTitleBinding =
                    DataBindingUtil.inflate(
                        layoutInflater,
                        R.layout.item_recommend_title,
                        null,
                        false
                    )
                return RecommendTitleViewHolder(binding)
            }
            2 -> {
                val binding: ItemMyChannelsBinding =
                    DataBindingUtil.inflate(layoutInflater, R.layout.item_my_channels, null, false)
                if (isEdit) {
                    binding.ivDelete.visibility = View.VISIBLE
                } else {
                    binding.ivDelete.visibility = View.GONE
                }
                binding.tvContent.setOnLongClickListener(View.OnLongClickListener {
                    if (isEdit) {
                        val rec: RecyclerView = parent as RecyclerView
                        val view: View = rec.getChildAt(0)
                        if (view == rec.layoutManager?.findViewByPosition(0)) {
                            val tvEdit: TextView = view.findViewById(R.id.tv_edit)
                            tvEdit.text = "完成"
                        }
                    }
                    mItemHelper.startDrag(MyChannelsViewHolder(binding))
                    return@OnLongClickListener true
                })
                return MyChannelsViewHolder(binding)
            }
            else -> {
                val binding: ItemChannelsRecommendBinding =
                    DataBindingUtil.inflate(
                        layoutInflater,
                        R.layout.item_channels_recommend,
                        null,
                        false
                    )
                return ChannelsRecommendViewHolder(binding)
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is MyTitleViewHolder -> {
                val binding: ItemMyTitleBinding = DataBindingUtil.getBinding(holder.itemView)!!
                binding.tvTitleGray.text = "点击进入频道"
            }
            is RecommendTitleViewHolder -> {
                val binding: ItemRecommendTitleBinding =
                    DataBindingUtil.getBinding(holder.itemView)!!
                binding.tvTitle.text = "推荐频道"
                binding.tvTitleGray.text = "点击添加频道"
            }
            is MyChannelsViewHolder -> {
                val binding: ItemMyChannelsBinding = DataBindingUtil.getBinding(holder.itemView)!!
                if (position==1||position==2){
                    binding.tvContent.setTextColor(Color.parseColor("#e9472c"))
                }
                binding.ivDelete.setOnClickListener {
                    if (isEdit) {
                        if (holder.bindingAdapterPosition > 2) {
                            myToRecommend(holder.bindingAdapterPosition)
                        }
                    }
                }
                binding.tvContent.setOnTouchListener(View.OnTouchListener { _, p1 ->
                    if (MotionEventCompat.getActionMasked(p1) == MotionEvent.ACTION_MOVE && isEdit && position != 1 && position != 2) {
                        mItemHelper.startDrag(holder)
                    }
                    return@OnTouchListener false
                })
                binding.data = myChannels[position - 1]
                binding.executePendingBindings()
            }
            else -> {
                val binding: ItemChannelsRecommendBinding =
                    DataBindingUtil.getBinding(holder.itemView)!!
                binding.ivAdd.setOnClickListener {
                    if (isEdit) {
                        recommendToMy(holder.bindingAdapterPosition)
                    }
                }
                binding.data = channelsRecommend[position - myChannels.size - 2]
                binding.executePendingBindings()
            }
        }
    }

    override fun getItemCount(): Int = myChannels.size + channelsRecommend.size + 2

    override fun getItemViewType(position: Int): Int {
        return if (position == 0) {
            0
        } else if (myChannels.size + 1 == position) {
            1
        } else if (position > 0 && position < myChannels.size + 1) {
            2
        } else {
            3
        }
    }

    class MyTitleViewHolder(binding: ItemMyTitleBinding) : RecyclerView.ViewHolder(binding.root) {
    }

    class RecommendTitleViewHolder(binding: ItemRecommendTitleBinding) :
        RecyclerView.ViewHolder(binding.root) {
    }

    class MyChannelsViewHolder(binding: ItemMyChannelsBinding) :
        RecyclerView.ViewHolder(binding.root) {
    }

    class ChannelsRecommendViewHolder(binding: ItemChannelsRecommendBinding) :
        RecyclerView.ViewHolder(binding.root) {
    }

    /**
    编辑状态
     */
    private fun starEdit(parent: RecyclerView) {
        isEdit = true
        for (i: Int in 0 until myChannels.size) {
            val view: View = parent.getChildAt(i + 1)
            if (i + 1 > 2) {
                val ivEdit: ImageView = view.findViewById(R.id.iv_delete)
                ivEdit.visibility = View.VISIBLE
            }
        }
    }

    private fun canceEdit(parent: RecyclerView) {
        isEdit = false
        for (i: Int in 0 until myChannels.size) {
            val view: View = parent.getChildAt(i + 1)
            val ivEdit: ImageView = view.findViewById(R.id.iv_delete)
            ivEdit.visibility = View.GONE
        }
    }

    /**
     * 移动到推荐
     */
    private fun myToRecommend(position: Int) {
        val starPosition: Int = position - 1
        if (starPosition > myChannels.size - 1) {
            return
        }
        val item: TabChModel = myChannels[starPosition]
        myChannels.removeAt(starPosition)
        channelsRecommend.add(0, item)
        notifyItemMoved(position, myChannels.size + 2)
    }

    /**
     * 移动到我的
     */
    private fun recommendToMy(position: Int) {
        val starPosition: Int = position - myChannels.size - 2
        if (starPosition > channelsRecommend.size - 1) {
            return
        }
        val item: TabChModel = channelsRecommend[starPosition]
        channelsRecommend.removeAt(starPosition)
        myChannels.add(item)
        notifyItemMoved(position, myChannels.size)
    }

    override fun onItemMove(fromPosition: Int, toPosition: Int) {
        Log.e("onItemMove", "$fromPosition $toPosition")
        val mTabChModel = myChannels[fromPosition - 1]
        myChannels.removeAt(fromPosition - 1)
        myChannels.add(toPosition - 1, mTabChModel)
        notifyItemMoved(fromPosition, toPosition)
    }
}